1 using System.Collections.Generic;
2
3 namespace ProceduralToolkit.Examples
4 {
5 /// <summary>
6 /// Maze graph representation
7 /// </summary>
8 public class Maze
9 {
10 private int width;
11 private int height;
12 private Directions[,] cells;
13
14 public Maze(int width, int height)
15 {
16 this.width = width;
17 this.height = height;
18 cells = new Directions[width, height];
19 }
20
21 public Directions this[Cell cell]
22 {
23 get { return cells[cell.x, cell.y]; }
24 set { cells[cell.x, cell.y] = value; }
25 }
26
27 public bool Contains(Cell cell)
28 {
29 return cell.x >= 0 && cell.x < width && cell.y >= 0 && cell.y < height;
30 }
31
32 public void AddEdge(Edge edge)
33 {
34 this[edge.origin] |= edge.origin.direction;
35 this[edge.exit] = edge.exit.direction;
36 }
37
38 public List<Edge> GetEdges(Cell origin)
39 {
40 var edges = new List<Edge>();
41 if (origin.direction != Directions.Left)
42 {
43 var edge = new Edge(origin.x, origin.y, Directions.Right, origin.depth);
44 if (Contains(edge.exit) && this[edge.exit] == Directions.None)
45 {
46 edges.Add(edge);
47 }
48 }
49 if (origin.direction != Directions.Right)
50 {
51 var edge = new Edge(origin.x, origin.y, Directions.Left, origin.depth);
52 if (Contains(edge.exit) && this[edge.exit] == Directions.None)
53 {
54 edges.Add(edge);
55 }
56 }
57 if (origin.direction != Directions.Down)
58 {
59 var edge = new Edge(origin.x, origin.y, Directions.Up, origin.depth);
60 if (Contains(edge.exit) && this[edge.exit] == Directions.None)
61 {
62 edges.Add(edge);
63 }
64 }
65 if (origin.direction != Directions.Up)
66 {
67 var edge = new Edge(origin.x, origin.y, Directions.Down, origin.depth);
68 if (Contains(edge.exit) && this[edge.exit] == Directions.None)
69 {
70 edges.Add(edge);
71 }
72 }
73 return edges;
74 }
75 }
76 }